package com.kma.summer2012.client.ExceptionHandling;

import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
import com.google.gwt.user.client.rpc.InvocationException;
import com.kma.summer2012.client.DialogBoxFactory;

/**
 * Created with IntelliJ IDEA.
 * User: Naskrina
 * Date: 09.03.13
 * Time: 11:46
 * To change this template use File | Settings | File Templates.
 */
public class ExceptionProcessor {
    private static ExceptionProcessor myself;

    private ExceptionProcessor() {
    }

    public static ExceptionProcessor getInstance() {
        if (myself == null) {
            myself = new ExceptionProcessor();
        }
        return myself;
    }

    public void processServiceFailure(Throwable problem) {

        try {
            throw problem;
        }
        catch (CrumbsRPCServiceException e){
            String name="Exception occurred";
            String message=e.getMessage();
            DialogBoxFactory.createMessageBox(message, name);
        }

        catch (IncompatibleRemoteServiceException e) {
            String name="Exception occurred";
          //  String message="Refresh the page, please.";
            String message= problem.getMessage()+"Refresh the page, please.";
            DialogBoxFactory.createUnclousableMessageBox(message,name);
            // JavaScript code running in the browser is a different version from JavaSCript deployed on the server.
            // this client is not compatible with the server; cleanup and refresh the browser
        }

        catch (InvocationException e) {
            String name="Exception occurred";
            String message="Network failure. Check your network connection and refresh the page.";
            DialogBoxFactory.createMessageBox(message,name);
            // the call didn't complete cleanly
            // other Throwables may be caught here...
        }

        catch (Throwable e) {
            String name="Exception occurred";
            String message="Connection to Data Base failure. Check your connection and try again later";
            DialogBoxFactory.createMessageBox(message,name);
            // last resort -- a very unexpected exception
        }
    }
}
